
/****************************************************************************/
/*Copyright (c) 2011, Florent DEVILLE.                                      */
/*All rights reserved.                                                      */
/*                                                                          */
/*Redistribution and use in source and binary forms, with or without        */
/*modification, are permitted provided that the following conditions        */
/*are met:                                                                  */
/*                                                                          */
/* - Redistributions of source code must retain the above copyright         */
/*notice, this list of conditions and the following disclaimer.             */
/* - Redistributions in binary form must reproduce the above                */
/*copyright notice, this list of conditions and the following               */
/*disclaimer in the documentation and/or other materials provided           */
/*with the distribution.                                                    */
/* - The names of its contributors cannot be used to endorse or promote     */
/*products derived from this software without specific prior written        */
/*permission.                                                               */
/* - The source code cannot be used for commercial purposes without         */ 
/*its contributors' permission.                                             */
/*                                                                          */
/*THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS       */
/*"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT         */
/*LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS         */
/*FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE            */
/*COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,       */
/*INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,      */
/*BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;          */
/*LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER          */
/*CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT        */
/*LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN         */
/*ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE           */
/*POSSIBILITY OF SUCH DAMAGE.                                               */
/****************************************************************************/


#include "menuOptionsGlobalAmbient.h"
#include <QLineEdit>
#include <QPushButton>
#include <QLabel>
#include <QGridLayout>
#include <QHBoxLayout>
#include <QGroupBox>

#include "Scene.h"

//constructor
menuOptionsGlobalAmbient::menuOptionsGlobalAmbient(QWidget* parent) : QWidget(parent)
{
	setWindowTitle("Global Ambient Light");

	//ambient light
	QLabel* lbR = new QLabel("R=");
	QLabel* lbG = new QLabel("G=");
	QLabel* lbB = new QLabel("B=");
	QLabel* lbA = new QLabel("A=");
	_leR = new QLineEdit();
	_leG = new QLineEdit();
	_leB = new QLineEdit();
	_leA = new QLineEdit();
	QHBoxLayout* ambientLayout = new QHBoxLayout();
	ambientLayout->addWidget(lbR);
	ambientLayout->addWidget(_leR);
	ambientLayout->addWidget(lbB);
	ambientLayout->addWidget(_leB);
	ambientLayout->addWidget(lbG);
	ambientLayout->addWidget(_leG);
	ambientLayout->addWidget(lbA);
	ambientLayout->addWidget(_leA);
	QGroupBox* groupAmbient = new QGroupBox("Global Ambient Light");
	groupAmbient->setLayout(ambientLayout);

	//button
	_buttonOK = new QPushButton("OK");
	_buttonDiscard = new QPushButton("Discard");
	QHBoxLayout* buttonLayout = new QHBoxLayout();
	buttonLayout->addWidget(_buttonOK);
	buttonLayout->addWidget(_buttonDiscard);
	QGroupBox* groupButton = new QGroupBox("");
	groupButton->setLayout(buttonLayout);

	//layout of the frame
	QGridLayout* frameLayout = new QGridLayout();
	frameLayout->addWidget(groupAmbient, 0, 0, 1, 1);
	frameLayout->addWidget(groupButton, 1, 0, 1, 1, Qt::AlignRight);

	setLayout(frameLayout);

	//button connection
	connect(_buttonOK, SIGNAL(clicked()), this, SLOT(OK()));
	connect(_buttonDiscard, SIGNAL(clicked()), this, SLOT(Discard()));

	//modal window
	setWindowModality(Qt::ApplicationModal);
}

//destructor
menuOptionsGlobalAmbient::~menuOptionsGlobalAmbient(){}

//show the frame and display the current global ambient light
void menuOptionsGlobalAmbient::fillAndShow()
{
	float* ambient = SCENE.getGlobalAmbientLight();
	_leR->setText(QString::number(ambient[0]));
	_leG->setText(QString::number(ambient[1]));
	_leB->setText(QString::number(ambient[2]));
	_leA->setText(QString::number(ambient[3]));

	show();
}

//set the global ambient light
void menuOptionsGlobalAmbient::setGlobalAmbientLight()
{
	float r = _leR->text().toFloat();
	float g = _leG->text().toFloat();
	float b = _leB->text().toFloat();
	float a = _leA->text().toFloat();

	SCENE.setGlobalAmbientLight(r, g, b, a);
}

//click on the ok button
void menuOptionsGlobalAmbient::OK()
{
	setGlobalAmbientLight();
	hide();
}

//click on the discard button
void menuOptionsGlobalAmbient::Discard()
{
	hide();
}